var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);

// PC端 保存商品规格
router.get('/jkyx/pc/save_spec', function(req, res, next) {
	var param = req.query || req.params; 
	var goods_id = param.goods_id || 0;
	var id = param.id;
	var price = param.price || 0;
	price = price * 100;
	var origprice = param.origprice || 0;
	origprice = origprice * 100;
	var spec = param.spec || '';
	var salecount = param.salecount || 0;
	var erpcode = param.erpcode || '';
	var stock = param.stock || 0;
	var sort = param.sort || '';
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_save_spec,[price,origprice,spec,salecount,erpcode,stock,sort,id,goods_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var spec = results[1];
				var len = spec.length;
				for (var i = 0; i < len; i ++) {
					spec[i].price = spec[i].price / 100;
					spec[i].origprice = spec[i].origprice / 100;
				}
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			spec: spec
	     		}));
			}
		})
	})
});
// PC端 删除商品规格
router.get('/jkyx/pc/delete_spec', function(req, res, next) {
	var param = req.query || req.params; 
	var id = param.id;
	var goods_id = param.goods_id || 0;
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_delete_spec,[id,goods_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var spec = results[1];
				var len = spec.length;
				for (var i = 0; i < len; i ++) {
					spec[i].price = spec[i].price / 100;
					spec[i].origprice = spec[i].origprice / 100;
				}
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			spec: spec
	     		}));
			}
		})
	})
});
// PC端 新增商品规格
router.get('/jkyx/pc/add_spec', function(req, res, next) {
	var param = req.query || req.params; 
	var goods_id = param.goods_id || 0;
	var price = param.price || 0;
	price = price * 100;
	var origprice = param.origprice || 0;
	origprice = origprice * 100;
	var spec = param.spec || '';
	var salecount = param.salecount || 0;
	var erpcode = param.erpcode || '';
	var stock = param.stock || 0;
	var sort = param.sort || '';
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_new_spec,[goods_id,price,origprice,spec,salecount,erpcode,stock,'1',sort,CommonJS.getNowFormatDate(),goods_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var spec = results[1];
				var len = spec.length;
				for (var i = 0; i < len; i ++) {
					spec[i].price = spec[i].price / 100;
					spec[i].origprice = spec[i].origprice / 100;
				}
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			spec: spec
	     		}));
			}
		})
	})
});



module.exports = router;
